package ${packagePrefix}.adapter.web.${tableName?lower_case};

import ${packagePrefix}.client.dto.command.${domainObjectName}AddCmd;
import ${packagePrefix}.client.dto.command.${domainObjectName}ModifyCmd;
import ${packagePrefix}.client.dto.data.${domainObjectName}DTO;
import ${packagePrefix}.client.dto.query.${domainObjectName}GetQry;
import ${packagePrefix}.client.dto.query.${domainObjectName}ListQry;
import com.alibaba.cola.dto.MultiResponse;
import com.alibaba.cola.dto.Response;
import com.alibaba.cola.dto.SingleResponse;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.Arrays;
import java.util.Collections;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

@WebMvcTest(${domainObjectName}Controller.class)
public class ${domainObjectName}ControllerTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @MockBean
    private ${domainObjectName}Controller ${domainObjectName?uncap_first}Controller;

    private ObjectMapper objectMapper = new ObjectMapper();

    @BeforeEach
    void setUp() {
        mockMvc = MockMvcBuilders.webAppContextSetup(context).build();
    }

    @Test
    void shouldAddSuccessfully() throws Exception {
        // Given
        ${domainObjectName}AddCmd addCmd = new ${domainObjectName}AddCmd();
        // Set properties for addCmd
        
        SingleResponse<Long> response = SingleResponse.of(1L);
        when(${domainObjectName?uncap_first}Controller.add(any(${domainObjectName}AddCmd.class))).thenReturn(response);

        // When & Then
        mockMvc.perform(post("/api/${tableName?lower_case}")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(addCmd)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andExpect(jsonPath("$.data").value(1L));
    }

    @Test
    void shouldUpdateSuccessfully() throws Exception {
        // Given
        ${domainObjectName}ModifyCmd modifyCmd = new ${domainObjectName}ModifyCmd();
        modifyCmd.setId(1L);
        // Set properties for modifyCmd
        
        Response response = Response.buildSuccess();
        when(${domainObjectName?uncap_first}Controller.update(any(${domainObjectName}ModifyCmd.class))).thenReturn(response);

        // When & Then
        mockMvc.perform(put("/api/${tableName?lower_case}")
                .contentType(MediaType.APPLICATION_JSON)
                .content(objectMapper.writeValueAsString(modifyCmd)))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true));
    }

    @Test
    void shouldGetByIdSuccessfully() throws Exception {
        // Given
        ${domainObjectName}DTO dto = new ${domainObjectName}DTO();
        dto.setId(1L);
        // Set properties for dto
        
        SingleResponse<${domainObjectName}DTO> response = SingleResponse.of(dto);
        when(${domainObjectName?uncap_first}Controller.getById(any(${domainObjectName}GetQry.class))).thenReturn(response);

        // When & Then
        mockMvc.perform(get("/api/${tableName?lower_case}/1")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andExpect(jsonPath("$.data.id").value(1L));
    }

    @Test
    void shouldListSuccessfully() throws Exception {
        // Given
        ${domainObjectName}DTO dto = new ${domainObjectName}DTO();
        dto.setId(1L);
        // Set properties for dto
        
        MultiResponse<${domainObjectName}DTO> response = MultiResponse.of(Collections.singletonList(dto));
        when(${domainObjectName?uncap_first}Controller.list(any(${domainObjectName}ListQry.class))).thenReturn(response);

        // When & Then
        mockMvc.perform(get("/api/${tableName?lower_case}")
                .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.success").value(true))
                .andExpect(jsonPath("$.data").isArray())
                .andExpect(jsonPath("$.data[0].id").value(1L));
    }
} 